<?php
session_start();
include "mysqli_connection.php";

if (!isset($_SESSION["username"])){
	echo "<script>window.location.href='login.php'</script>";
}
else{
	$query = "SELECT Group_Requirement.g_id, Groups.name, Groups.number FROM Group_Requirement INNER JOIN Groups WHERE (r_id = ? ) AND (Groups.g_id = Group_Requirement.g_id);";
	$stmt = $mysqli->stmt_init();
	if ($stmt = $mysqli->prepare($query)){
		$stmt->bind_param("i", trim($_POST["r_id"]));
		$stmt->execute();
		$stmt->bind_result($g_id, $name, $number);
	}
	$text = "";
	if (isset($stmt)){
		while ($stmt->fetch()){
			if ($text === ""){
				$text .= "<div><ul>";
			}
			$text .= "<li>" . $number . " of <a href = 'managegroups.php?index=" . $g_id . "'>" . $name . "</a></li>";
		}
		$stmt->close();
	}
	if ($text !== ""){
		echo $text . "</ul></div>";
	}
	else{
		echo "<div>No groups</div>";
	}
}